<?php


use App\Library\Utils\Arr;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class LearningSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'CourseSeeder',
            'ChapterSeeder',
            'UserSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $courses = $this->fetchAll('SELECT * FROM kg_course');
        $chapters = $this->fetchAll('SELECT * FROM kg_chapter');
        $users = $this->fetchAll('SELECT * FROM kg_user');

        $rows = [];
        for($i=0;$i<500;$i++) {
            $rows[] = [
                'request_id'=>$faker->uuid,
                'course_id'=>Arr::rand($courses)['id'],
                'chapter_id'=>Arr::rand($chapters)['id'],
                'user_id'=>Arr::rand($users)['id'],
                'plan_id'=>$faker->date('Ymd'),
                'duration'=>rand(0,500),
                'position'=>0,
                'client_type'=>1,
                'client_ip'=>$faker->ipv4,
                'active_time'=>$faker->unixTime,
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_learning')->insert($rows)->saveData();
    }
}
